﻿using System;
public abstract class Shape {
    public abstract double CalculateArea();
    public abstract bool isValid();
}
public class Rectangle:Shape {
    private double length;
    private double width;
    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }
    public override double CalculateArea() {
        return length * width;
    }
    public override bool isValid() {
        if (length > 0 && width > 0)
        {
            if (length == width)
            {
                Console.WriteLine("这是正方形");
                return true;
            }
            else {
                Console.WriteLine("这是长方形");
                return true;
            }
        }
        else {
            Console.WriteLine("输入值不合法");
            return false;
        }
        
    }
}
public class Circle : Shape {
    private double r;
    public Circle(double r) {
        this.r = r;
    }
    public override double CalculateArea()
    {
        return r * r*3.14;
    }
    public override bool isValid() {
        if (r > 0)
        {
            Console.WriteLine("这是圆形");
            return true;
        }
        else {
            Console.WriteLine("输入值不合法");
            return false;
        }
        
    }
}
public class EqualTrangle : Shape {
    private double length;
    public EqualTrangle(double length)
    {
        this.length = length;
    }
    public override double CalculateArea()
    {
        return Math.Sqrt(3) * length * length / 4; 
    }
    public override bool isValid() {
        if (length > 0)
        {
            Console.WriteLine("这是等边三角形");
            return true;
        }
        else {
            Console.WriteLine("输入值不合法");
            return false;
        }
    }
}
namespace ShapeApplication {
    class Program { 
        static void Main(string[] args)
        {
            double sumArea = 0;
            Random rand = new Random();
            List<Shape> list = new List<Shape>();
            for (int i = 0; i < 10; i++)
            {
                int index=rand.Next(0,3);
                switch (index){
                    case 0:
                        double length1 = (rand.NextDouble() - 0.3) * 10;
                        double width = (rand.NextDouble() - 0.3) * 10;
                        list.Add(new Rectangle(length1, width));
                        break;
                    case 1:
                        double r= (rand.NextDouble() - 0.3) * 10;
                        list.Add(new Circle(r));
                        break;
                    case 2:
                        double length2= (rand.NextDouble() - 0.3) * 10;
                        list.Add(new EqualTrangle(length2));
                        break;
                }

            }
            for (int i = 0; i < 10; i++) {
                if (list[i].isValid()) {
                    sumArea += list[i].CalculateArea();
                }
            }
            Console.WriteLine("合法图形的总面积为"+sumArea);
        }
    }
}
